package lan.battletank.element;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import lan.battletank.environment.EnvConst;

public class BaseBattleUnitElement extends BaseElement {
	
	public static int TANK_WIDTH = 60;
    public static int TANK_HEIGHT = 40;
    public boolean isAlive; // tank is alive
    public boolean isMoving; // whether tank is moving
    public boolean isAdjusting; // whether tank is changing the cannon angle
    public boolean isFalling; // whether tank is fall from higher ground
    public boolean isFallingL; // tank is falling to the left abyss
    public boolean isFallingR; // tank is falling to the right abyss
    public boolean leftStable; // whether the left part of tank is on ground
    public boolean rightStable; // whether the right part of tank is on ground
    
    public boolean isPlayer = false;
    
    protected int mPhase; // The explosion phase
    protected int mPeriodCounter; // Counter of each explosion phase

    /** Tank cannon gun position. */
    protected int gunX;
    protected int gunY;

    /** The angle(degree) of the cannon. */
    protected float mCannonAngle; // range: 0 - 90
    protected boolean prepareToFire; // tank is preparing to fire
    protected boolean mIsFiring; // whether tank is firing a bullet
    protected boolean mShowFireEffect; // whether to render the tank fire effect

    protected static final int ALL_PHASE = 10; // Tank fire all phase to render
    protected static final int PERIOD = 1; // Lasting period of every phase
    protected static final int FIRE_POINT_OFFSET_Y = 75; // to place the fire effect center at the gun point
    
    protected Bitmap[] mFireImages = new Bitmap[ALL_PHASE]; // ank fire image to use
    /** Pixel width and hight of explosion image. */
    protected int mFireWidth;
    protected int mFireHeight;
    
    protected int hp = EnvConst.HP; // hit point
    protected boolean isHit = false; // if unit been hit by bullet

    public void draw(Canvas c, BitmapDrawable unitImg, Paint p) {
    	if (isAlive) {
	    	unitImg.setBounds((int) centerX - halfWidth, (int) centerY - halfHeight, (int) centerX + halfWidth, (int) centerY + halfHeight);
	    	unitImg.draw(c);
	    	if (isPlayer) {
	    		c.drawText(String.valueOf((int) mCannonAngle), (int) centerX, (int) centerY - halfHeight, p);
	    	}
	        c.drawText(String.valueOf(hp), (int) centerX, (int) centerY - halfHeight - 20, p);
	        // onTouch is thread different from render thread, so there may be a timing issue that mShowFireEffect is true, but mPhase is still 0
	        if (mShowFireEffect && mPhase > 0) {
	            c.drawBitmap(mFireImages[mPhase - 1], gunX - mFireWidth / 2, gunY - FIRE_POINT_OFFSET_Y, null);
	        }
    	}
    }
    
    public void takeDamage(int d) {
        hp -= d;
        if (hp <= 0) {
            hp = 0;
            isAlive = false;
        }
    }

    public int getHP() {
        return hp;
    }
    
	public void setHit(boolean b) {
		isHit = b;
	}
	
	public boolean isHit() {
		return isHit;
	}
	
	public void actionEnd() {
        mIsFiring = false;
        isMoving = false;
        isAdjusting = false;
        isFalling = false;
        leftStable = true;
        rightStable = true;
        isFallingL = false;
        isFallingR = false;
    }

	public boolean isFiring() {
        return mIsFiring;
    }
}
